#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
ll e[2050][2050], dist[2050];
bool st[2050];

ll gcd(ll a, ll b)//求公约数
{
    return a % b == 0 ? b : gcd(b, a % b);
}

ll lcm(ll a, ll b)//求最小公倍数
{
    return a * b / (gcd(a, b));
}

int dijkstra() {
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    for (int i = 1; i <= 2021; ++i) {
        int t = -1;
        for (int j = 1; j <= 2021; ++j) {
            if (!st[j] && (t == -1 || dist[t] > dist[j])) {
                t =  j;
            }
        }
        st[t] = true;
        for (int j = 1; j <= 2021; ++j) {
            dist[j] = min(dist[j], dist[t] + e[t][j]);
        }
    }
    return dist[2021];
}

int main() {
    memset(e, 0x3f, sizeof e);
    for (int i = 1; i <= 2021; i++) {
        for (int j = i; j <= 2021; j++) {
            int k = j - i;
            if (k > 21) {
                break;
            } else {
                e[i][j] = e[j][i] = lcm(i, j);
            }
        }
    }
    int ans = dijkstra();
    cout << ans << endl;
}
